﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Data.OleDb;
namespace DAO
{
    public class TrongTaiDAO
    {
        public static string LayDonVi()
        {
            string donVi = String.Empty;
            try
            {
                OleDbConnection cnn = BoKetNoiCSDL.KetNoi();
                OleDbCommand cmd = new OleDbCommand("SELECT DonVi FROM TRONG_TAI", cnn);
                try
                {
                    donVi = (string)cmd.ExecuteScalar();
                    cnn.Close();
                }
                catch (System.Exception ex)
                {
                	if(cnn.State == ConnectionState.Open)
                    {
                        cnn.Close();
                    }
                    throw ex;
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return donVi;
        
        }

        public static List<TrongTaiDTO> LayDanhSach()
        {
            List<TrongTaiDTO> lstTrongTai = null;
            try
            {
                OleDbConnection cnn = BoKetNoiCSDL.KetNoi();
                OleDbCommand cmd = new OleDbCommand("SELECT * FROM TRONG_TAI", cnn);
                try
                {
                    OleDbDataReader rd = cmd.ExecuteReader();
                    lstTrongTai = new List<TrongTaiDTO>();
                    while(rd.Read())
                    {
                        TrongTaiDTO trongTai = new TrongTaiDTO();
                        trongTai.MaTrongTai = (int)rd["MaTrongTai"];
                        trongTai.GiaTriTrongTai = (double)rd["GiaTriTT"];
                        trongTai.DonVi = (string)rd["DonVi"];
                        lstTrongTai.Add(trongTai);
                    }
                    cnn.Close();
                }
                catch (System.Exception ex)
                {
                	if(cnn.State == ConnectionState.Open)
                    {
                        cnn.Close();
                    }
                    throw ex;
                }
            }
            catch (System.Exception ex)
            {
            	throw ex;
            }
            return lstTrongTai;
        }
    }
}
